import { axiosInstance } from 'src/boot/axios'
import CommonConfig from '../../assets/js/config'

import qs from 'qs'

const qsConfig = {
  arrayFormat: 'repeat',
  skipNulls: true,
  format: 'RFC3986'
}

const machinestatusUrl = CommonConfig.machinestatusUrl + '/v1'

/**
 * 获取报警明细
 * @deviceIds 设备id数组
 * @start 开始时间
 * @end 结束时间
 * @sortValue 排序，desc：降序, asc：升序
 * @pageIndex 当前页
 * @pageSize 每页多少条
 * @returns {Promise<AxiosResponse<any>>}
 */
export function getAlarmList (params) {
  const newUrl = `${machinestatusUrl}/devices/device_alarms`
  let reqData = {
    params,
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  return axiosInstance.post(newUrl, reqData.params).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 根据设备id 列举报警统计
 * @param params
 * @returns {Promise<AxiosResponse<any>>}
 */
export function getAlarmChartData (params) {
  const newUrl = `${machinestatusUrl}/devices/device_alarms:statistics`
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}

export function downloadAlarm (params) {
  const newUrl = `${machinestatusUrl}/devices/device_alarms/export/excel`
  // const newUrl = `${machinestatusUrl}/devices/device_alarms`
  let reqData = {
    params,
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  return axiosInstance.post(newUrl, reqData.params).then((res) => {
    return Promise.resolve(res)
  })
}
